//
// Created by harrybao on 2019/4/2.
//

#ifndef CMAKE_TIMER_MANAGER_H
#define CMAKE_TIMER_MANAGER_H
#include <cstdint>
#include <vector>

using HTIMER = uint64_t;

class ListNode;
class TimerManager
{
public:
    bool Init();
    void UnInit();
    void Update(int32_t delta_time);
    HTIMER AddTimer(int32_t value, int64_t interval, uint32_t count);
    void KillTimer(HTIMER &timer_uid);
public:
    void Debug();
private:
    class TimerCell;
    class TimerWheel;
private:
    void UpdateWheel(int32_t index);
    TimerCell* GetFreeTimerCell();
    void RecycleTimerCell(TimerCell* timer_cell);
    TimerCell* FindTimerCell(HTIMER timer_uid);
    void InsertTimer(TimerCell* timer_cell);
    void DeleteTimer(TimerCell* timer_cell);

    int64_t now_time_{ 0 };
    int64_t nex_time_{ 0 };
    uint32_t pos_{ 0 };
    ListNode* free_cell_head_{ nullptr };
    std::vector<TimerCell*> cell_pool_;
    TimerWheel* timer_wheels_{ nullptr };
};

#endif //CMAKE_TIMER_MANAGER_H
